package sapling.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sapling.dao.MarkorderDao;
import sapling.dao.TreeDao;
import sapling.model.Markorder;
import sapling.model.OutboundOrder;
import sapling.model.Tree;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2017/9/11.
 */
@Service
@Transactional
public class PutOutService {
    @Autowired
    TreeDao treeDao;
    @Autowired
    MarkorderDao markorderDao;

    public List<Tree> put() {
        List<Tree> trees = treeDao.findResstate(0);
        List<Tree> treess = new ArrayList<>();
        for (Tree value:trees) {
            List<Tree> trees1=treeDao.findSerialnumberAndPrice(value.getSerialNumber(),0);
            if(Objects.nonNull(trees1)&&trees1.size()>0){
                value.setNumber(trees1.size());
                treess.add(value);
            }
        }
       return treess;
    }

    public List<Tree> treeList(List<OutboundOrder> outStatus,int type){
        List<Tree> treeList=new ArrayList<>();
        for(OutboundOrder out:outStatus){
            if(Objects.nonNull(out.getTrees())&&out.getTrees().size()>0){
                Tree tree = out.getTrees().get(0);
                tree.setTree_id(out.getOutboundOrder_id());
                int number=0;
                for (Tree value:out.getTrees()) {
                    if(type==value.getSalesState()){
                        number++;
                    }
                }
                tree.setNumber(number);
                if(Objects.nonNull(out.getTransactionAmount())){
                    tree.setPrice(out.getTransactionAmount().doubleValue());
                }else {
                    tree.setPrice(0);
                }
                tree.setCreatetime(out.getGmt_payment());
                tree.setUsername(out.getName());
                if(number>0){
                    treeList.add(tree);
                }
            }
        }
        return treeList;
    }


}
